package org.u2ml.app.domain;

import java.util.ArrayList;
import java.util.Collection;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("response")
public class RESTResponse {
	@XStreamAsAttribute
	private ResponseError error;
	@XStreamAsAttribute
	private ResponseWarning warning;
	@XStreamAsAttribute
	private Collection<ResponseData> data;
	
	public RESTResponse() {
		this.error = null;
		this.warning = null;
		this.data  = null;
	}
	
	public void setErrorCode(String code) {
		ResponseError error = new ResponseError();
		error.setCode(code);
		this.setError(error);
	}
	
	public void setWarningCode(String code) {
		ResponseWarning warning = new ResponseWarning();
		warning.setCode(code);
		this.setWarning(warning);
	}
	
	public ResponseError getError() {
		return error;
	}
	public void setError(ResponseError error) {
		this.error = error;
	}
	public ResponseWarning getWarning() {
		return warning;
	}
	public void setWarning(ResponseWarning warning) {
		this.warning = warning;
	}
	public Collection<ResponseData> getData() {
		return data;
	}
	public void setData(Collection<ResponseData> data) {
		this.data = data;
	}
	public void addData(ResponseData d) {
		if(this.data == null) {
			this.data = new ArrayList<ResponseData>();
		}
		this.data.add(d);
	}
	
	public void addAllData(Collection<? extends ResponseData> collection) {
		if (this.data == null)
			this.data = new ArrayList<ResponseData>();
		for(ResponseData data : collection) {
			this.data.add(data);
		}
	}
	
	@Override
	public String toString() {
		return "Response [error=" + error.toString() + ", warning=" + warning.toString() + ", data=" + data.toString() + "]";
	}
}
